﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using P.InOut.NetMvc.Models;
using P.InOut.NetMvc.Properties;

namespace P.InOut.NetMvc.Controllers
{
    /// <summary>
    /// Class provides controller for the Status Profile Component
    /// </summary>
    public class ProfileComponentController : ComponentController
    {
        /// <summary>
        /// Method gets the current Status for a User by ID
        /// </summary>
        public virtual void Status()
        {
            // Get User
            MembershipUser user = Membership.GetUser(this.Context.HttpContext.User.Identity.Name);

            using (InOutDataContext db =
                new InOutDataContext(Settings.Default.dbConn))
            {
                // Get Profile using user id
                Profile profile = db.Profiles.Single<Profile>(p => p.UserId == (Guid)user.ProviderUserKey);
                
                // Set ViewData Property
                var viewData = new ProfileStatusData
                {
                    Profile = profile
                };

                // Render
                RenderView("Status", viewData);
            }
        }

        /// <summary>
        /// Method gets the current Status for a User by ID
        /// </summary>
        public virtual void DetailStatus()
        {
            // Get User
            MembershipUser user = Membership.GetUser(this.Context.HttpContext.User.Identity.Name);

            using (InOutDataContext db =
                new InOutDataContext(Settings.Default.dbConn))
            {
                string id = this.Context.RouteData.Values["id"].ToString();
                // Get Profile using user id
                Profile profile = db.Profiles.Single<Profile>(p => p.UserId.ToString() == id);

                // Set ViewData Property
                var viewData = new ProfileStatusData
                {
                    Profile = profile
                };

                // Render
                RenderView("Status", viewData);
            }
        }
    }

    public class ProfileStatusData
    {
        public Profile Profile { get; set; }
    }
}
